package com.lagou.session;

import com.lagou.config.XMLConfigBuilder;
import com.lagou.io.Resources;
import com.lagou.pojo.Configuration;
import com.lagou.session.impl.DefaultSqlSessionFactory;

import java.io.InputStream;

public class SqlSessionFactoryBuilder {

    public static SqlSessionFactory build(String resource) throws Exception {
        // 解析xml配置文件, 解析结果封装入 Configuration
        InputStream inputStream = Resources.getResourceAsStream(resource);
        XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder(inputStream);
        Configuration configuration = xmlConfigBuilder.parse();
        // 创建 sqlSession 工厂类
        DefaultSqlSessionFactory sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
        return sqlSessionFactory;
    }
}
